package com.wangyadong.hobby.thread.lesson2withconcurrent;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class VolitileTest {

    private  int i = 0;
    private static final Map<Integer, Integer> map = new ConcurrentHashMap<>();

    public static void main(String[] args) throws InterruptedException {
        VolitileTest test = new VolitileTest();
        IntStream.range(0, 1000).forEach(i -> {
            new Thread(() -> {
                add(test);
            }).start();
        });
        TimeUnit.SECONDS.sleep(3);
        System.out.println(map.size() == 1000);

    }

    private static synchronized int add(VolitileTest test) {
        test.i += 1;
        map.put(test.i, test.i);
        return test.i;
    }
}
